package fr.inserm.umr915.vcfserver.bio;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;


/**
 * Position
 */
public  class Position
	implements Comparable<Position>
	{
	private String chromosome;
	private int position;

	
	public Position(String chromosome,int position)
		{
		this.chromosome=chromosome;
		this.position=position;
		}
	
	public int getPosition()
		{
		return position;
		}
	
	public String getChromosome()
		{
		return this.chromosome;
		}
	@Override
	public int compareTo(Position o)
		{
		int i=this.chromosome.compareTo(o.chromosome);
		if(i!=0) return i;
		return this.position-o.position;
		}
	
	
	
	@Override
	public int hashCode()
		{
		final int prime = 31;
		int result = 1;
		result = prime * result + chromosome.hashCode();
		result = prime * result + position;
		return result;
		}
	
	@Override
	public boolean equals(Object obj)
		{
		if (this == obj) { return true; }
		if (obj == null) { return false; }
		if (getClass() != obj.getClass()) { return false; }
		Position other = (Position) obj;
		
		if (position != other.position) { return false; }
		if(!chromosome.equals(other.chromosome)) { return false; }
		return true;
		}
	
	@Override
	public String toString()
		{
		return getChromosome()+":"+getPosition();
		}
	
	static public class Binding
	extends TupleBinding<Position>
		{
		@Override
		public Position entryToObject(TupleInput in)
			{
			String c= in.readString();
			int pos=in.readInt();
			return new Position(c,pos);
			}
		@Override
		public void objectToEntry(Position o, TupleOutput out)
			{
			out.writeString(o.chromosome);
			out.writeInt(o.position);
			}
		}
	}